package com.central.apps.handler.purchase;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.exception.service.ServiceException;
import com.central.common.util.ToolUtil;
import com.central.common.web.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 采购发票处理类
 *
 * @author 陈运祥
 * @date 2023/1/3
 */
@Component
@Slf4j
public class PurchaseinvoiceHandler extends BaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        int page = 1;
        int limit = 10;
        if (MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE) != null && MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT) != null) {
            page = MapUtil.getInt(params, AppConstant.BIZ_QUERY_PAGE);
            limit = MapUtil.getInt(params, AppConstant.BIZ_QUERY_LIMIT);
        }
        if ("purchaseinvoiceApp".equals(app)) {
            if ("purchaseinvoice".equals(type)) {
                String ecnum = MapUtils.getString(params, "ecnum");
                String where = "and "+" "+ MapUtils.getString(params, "where_condition");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT * from biz_attribute where object_id ='563'");
                //排序
                String columnname="";
                String string="";
                if (CollectionUtils.isNotEmpty(bySql1)){
                    for (int i=0;i<bySql1.size();i++){

                        Map<String, Object> map = bySql1.get(i);
                        columnname = MapUtils.getString(map, "columnname");
                        //字段名_orders: "asc"
                        String columnname1= columnname+"_orders";
                        String columnname11 = MapUtils.getString(params, columnname1);

                        if (StringUtils.isNotEmpty(columnname11)) {
                            string  = "order by "+columnname+" "+columnname11;
                        }
                    }
                }

                List<Map<String, Object>> bySql = null;
                String ponum = MapUtils.getString(params, "ponum");
                String description = MapUtils.getString(params, "description");
                ArrayList<String> strings = new ArrayList<>();
                strings.add(ponum);
                strings.add("ponum");
                strings.add(description);
                strings.add("description");
                String SQL = "and  t.id in (SELECT poid FROM gq_posline where commerceorder ='" + ecnum + "')";
                if (StringUtils.isNotEmpty(ecnum)) {
                    if (ponum == null && description == null) {
                        if (StringUtils.isEmpty(string)){
                            string ="order by t.ponum desc";
                        }
                        bySql = handlerService.findBySql(" select t.`suppliername` , t.`ponum` , t.`description` , t.`suppliernum` , t.`id` , t.`unpaidamt`,t.conname,t.concode from `gq_po` t WHERE t.`delflag` = '0' and t.status='已批准' and t.unpaidamt>0 " + SQL + "  "+where+ " "+string+" ");
                    } else {
                        if (StringUtils.isEmpty(string)){
                            string ="order by t.ponum desc";
                        }
                        String list = " t.";
                        String list1 = " like";
                        String list3 = "";
                        for (int i = 0; i < strings.size(); i = i + 2) {
                            String s = strings.get(i);
                            if (s != null) {
                                if (list3 == "") {
                                    list3 = list3 + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                } else {
                                    list3 = list3 + " and " + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                }
                            }
                        }
                        bySql = handlerService.findBySql(" select t.`suppliername` , t.`ponum` , t.`description` , t.`suppliernum` , t.`id` , t.`unpaidamt`,t.conname,t.concode from `gq_po` t WHERE " + list3 + " and t.`delflag` = '0' and t.status='已批准' and t.unpaidamt>0 " + SQL + "  "+where+ " "+string+" ");
                    }
                } else {
                    if (ponum == null && description == null) {
                        if (StringUtils.isEmpty(string)){
                            string ="order by t.ponum desc";
                        }
                        bySql = handlerService.findBySql(" select t.`suppliername` , t.`ponum` , t.`description` , t.`suppliernum` , t.`id` , t.`unpaidamt`,t.conname,t.concode from `gq_po` t WHERE t.`delflag` = '0' and t.status='已批准' and t.unpaidamt>0  "+where+ " "+string+" ");
                    } else {
                        if (StringUtils.isEmpty(string)){
                            string ="order by t.ponum desc";
                        }
                        String list = " t.";
                        String list1 = " like";
                        String list3 = "";
                        for (int i = 0; i < strings.size(); i = i + 2) {
                            String s = strings.get(i);
                            if (s != null) {
                                if (list3 == "") {
                                    list3 = list3 + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                } else {
                                    list3 = list3 + " and " + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                }
                            }
                        }
                        bySql = handlerService.findBySql(" select t.`suppliername` , t.`ponum` , t.`description` , t.`suppliernum` , t.`id` , t.`unpaidamt`,t.conname,t.concode from `gq_po` t WHERE " + list3 + " and t.`delflag` = '0' and t.status='已批准' and t.unpaidamt>0  "+where+ " "+string+" ");
                    }
                }
                if (CollectionUtils.isNotEmpty(bySql)) {
                    List<Map<String, Object>> collect = bySql.stream().skip((page - 1) * limit).limit(limit).collect(Collectors.toList());
                    return PageResult.<Map<String, Object>>builder().data(collect).code(0).count((long) bySql.size()).build();
                }
            } else if ("invoicepursummary".equals(type)) {
                String poid = MapUtils.getString(params, "poid");
                String id = MapUtils.getString(params, "id");
                String ponum = MapUtils.getString(params, "ponum");
                String ecnum = MapUtils.getString(params, "ecnum");
                String itemnum = MapUtils.getString(params, "itemnum");
                String itemdesc = MapUtils.getString(params, "itemdesc");
                String orderunit = MapUtils.getString(params, "orderunit");
                String spemodel = MapUtils.getString(params, "spemodel");
                String unpaidamt = MapUtils.getString(params, "unpaidamt");
                String pinum = MapUtils.getString(params, "pinum");
                String subjectno="";
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select subjectno from gq_purchaseinvoice where pinum='" + pinum + "'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    Map<String, Object> map = bySql1.get(0);
                    subjectno = MapUtils.getString(map, "subjectno");
                }
                ArrayList<String> strings = new ArrayList<>();
                strings.add(itemnum);
                strings.add("itemnum");
                strings.add(itemdesc);
                strings.add("itemdesc");
                strings.add(orderunit);
                strings.add("orderunit");
                strings.add(spemodel);
                strings.add("spemodel");
                strings.add(unpaidamt);
                strings.add("unpaidamt");

                List<Map<String, Object>> bySql = null;
                String SQL = "and  t.commerceorder='" + ecnum + "'";
                String SQL1="and t.subjectno='"+subjectno+"'";
                if (StringUtils.isNotEmpty(ecnum)) {
                    if (StringUtils.isNotEmpty(subjectno)){
                        if (itemnum == null && itemdesc == null && orderunit == null && spemodel == null && unpaidamt == null) {
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') "+SQL1+" and t.unpaidamts>0 " + SQL + "  order by t.createtime");
                        } else {
                            String list = " t.";
                            String list1 = " like";
                            String list3 = "";
                            for (int i = 0; i < strings.size(); i = i + 2) {
                                String s = strings.get(i);
                                if (s != null) {
                                    if (list3 == "") {
                                        list3 = list3 + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    } else {
                                        list3 = list3 + " and " + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    }
                                }
                            }
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE " + list3 + " and t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') "+SQL1+" and t.unpaidamts>0 " + SQL + "  order by t.createtime");
                        }
                    }else {
                        if (itemnum == null && itemdesc == null && orderunit == null && spemodel == null && unpaidamt == null) {
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') and t.unpaidamts>0 " + SQL + "  order by t.createtime");
                        } else {
                            String list = " t.";
                            String list1 = " like";
                            String list3 = "";
                            for (int i = 0; i < strings.size(); i = i + 2) {
                                String s = strings.get(i);
                                if (s != null) {
                                    if (list3 == "") {
                                        list3 = list3 + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    } else {
                                        list3 = list3 + " and " + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    }
                                }
                            }
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE " + list3 + " and t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') and t.unpaidamts>0 " + SQL + "  order by t.createtime");
                        }
                    }

                } else {
                    if (StringUtils.isNotEmpty(subjectno)){
                        if (itemnum == null && itemdesc == null && orderunit == null && spemodel == null && unpaidamt == null) {
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') and t.unpaidamts>0  "+SQL1+" order by t.createtime");
                        } else {
                            String list = " t.";
                            String list1 = " like";
                            String list3 = "";
                            for (int i = 0; i < strings.size(); i = i + 2) {
                                String s = strings.get(i);
                                if (s != null) {
                                    if (list3 == "") {
                                        list3 = list3 + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    } else {
                                        list3 = list3 + " and " + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    }
                                }
                            }
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE " + list3 + " and t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') and t.unpaidamts>0  "+SQL1+" order by t.createtime");
                        }
                    }else {
                        if (itemnum == null && itemdesc == null && orderunit == null && spemodel == null && unpaidamt == null) {
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') and t.unpaidamts>0  order by t.createtime");
                        } else {
                            String list = " t.";
                            String list1 = " like";
                            String list3 = "";
                            for (int i = 0; i < strings.size(); i = i + 2) {
                                String s = strings.get(i);
                                if (s != null) {
                                    if (list3 == "") {
                                        list3 = list3 + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    } else {
                                        list3 = list3 + " and " + list + strings.get(i + 1) + list1 + "'%" + s + "%'";
                                    }
                                }
                            }
                            bySql = handlerService.findBySql("select t.`commerceorder` , t.`subjectno` , t.`unpaidamts` , t.`specifications` , t.`totalcost` , t.`n" +
                                    "umber` , t.`itemdesc` , t.`receivedqty` , t.`qualitygrade` , t.`model` , t.`isinspection` , " +
                                    "t.`id` , t.`uninspectqty` , t.`packform` , t.`createuserdesc` , t.`unreceivedqty` , t.`taxrate` , t.`invoicequantitys` , t.`amountexcludingtax` , " +
                                    "t.`version` , t.`ppid` , t.`itemnum` , t.`taxamount` ,t.totalcost,t.amexclutax, t.`orderqty` , t.`innum` , t.`status` , t.`receivedunitcost` , t.`returnqty` , t.`unitcost` , " +
                                    "t.`maintechtors` , t.`demount` , t.`receiptscomplete` , t.`deptid` , t.`deptcode` , t.`inspectqty` , t.`ppslid` , t.`amexclutax` ," +
                                    " t.`manufacturer` , t.`rejectedqty` , t.`qualquantity` , t.`unrejectedqty` , t.`itemid` , t.`proid` , t.`quantityrequired` , t.`pricetaxtotals` , " +
                                    "t.`purchasemethod` , t.`createtime` , t.`receivedtotalcost` , t.`orderunit` , t.`createuser` , t.`paidamts` , t.`poid` , t.`spemodel` , " +
                                    "t.`reqdeliverydate` , t.`ponum` , t.`receivedlinecost` , t.`updatetime` , t.`updateuser` from `gq_posline` t WHERE " + list3 + " and t.`delflag` = '0' " +
                                    "and t.poid='" + poid + "' and t.id not in (select poslid from gq_invoicepursummary where prtid = '" + id + "') and t.ponum ='" + ponum + "' and" +
                                    " t.poid in (select id from gq_po where status ='已批准') and t.unpaidamts>0  order by t.createtime");
                        }
                    }

                }
                if (CollectionUtils.isNotEmpty(bySql)) {
                    List<Map<String, Object>> collect = bySql.stream().skip((page - 1) * limit).limit(limit).collect(Collectors.toList());
                    return PageResult.<Map<String, Object>>builder().data(collect).code(0).count((long) bySql.size()).build();
                }
            }
        }
        return super.operate(app, type, params);
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        //计算含税金额，无税金额，税额；保留两位小数
        //  gq_invoicepursummary
        if ("gq_invoicepursummary".equals(objName)) {
            if (isUpdate==false){
                BigDecimal unitcost = new BigDecimal(MapUtils.getString(data, "unitcost"));//含税单价
                String pinum = MapUtils.getString(data, "pinum");
                Float invoicequantity1 = MapUtils.getFloat(data, "invoicequantity");
                String poslid1 = MapUtils.getString(data, "poslid");//采购订单会总行id
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select receivedqty from gq_posline where id='" + poslid1 + "'");//查询采购订单入库数量
                Float receivedqty=(float)0;
                if (CollectionUtils.isNotEmpty(bySql1)){
                    receivedqty = MapUtils.getFloat(bySql1.get(0), "receivedqty");
                }
                String status="已批准";
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select t.invoicequantity from gq_invoicepursummary t left join gq_purchaseinvoice f on t.pinum=f.pinum where t.poslid='" + poslid1 + "' and f.status='" + status + "'");
                Float invoicequantity2=(float)0;
                if (CollectionUtils.isNotEmpty(bySql2)){
                    for (int i=0;i<bySql2.size();i++){
                        Map<String, Object> map = bySql2.get(i);
                        Float invoicequantity = MapUtils.getFloat(map, "invoicequantity");
                        invoicequantity2=invoicequantity2+invoicequantity;//累计开发票数量
                    }
                }
                invoicequantity1=receivedqty-invoicequantity2;
                handlerService.updateBySql("update gq_invoicepursummary set invoicequantity ="+invoicequantity1+"  where poslid = " + poslid1 + " and pinum='"+pinum+"'");
                String invoicequantity3=Float.toString(invoicequantity1);//剩余发票数量
                BigDecimal invoicequantity = new BigDecimal(invoicequantity3);//发票数量
                //float taxamount = MapUtils.getFloat(data, "taxamount");//税额
                //float taxrate = MapUtils.getFloat(data, "taxrate");
                BigDecimal taxrate = new BigDecimal(MapUtils.getString(data, "taxrate"));//税额
                //含税金额
                BigDecimal pricetaxtotal = invoicequantity.multiply(unitcost);// unitcost * invoicequantity;//含税单价*发票数量
                //无税金额
                double suil = 1 + taxrate.doubleValue();
                BigDecimal nottaxamount = pricetaxtotal.divide(new BigDecimal(suil), 2, BigDecimal.ROUND_HALF_UP);//无税金额=含税金额/（1+税率）
                //float nottaxamount = pricetaxtotal-taxamount;//无税金额 = 含税金额-税额
                //税额
                BigDecimal taxamount = pricetaxtotal.subtract(nottaxamount);//pricetaxtotal - nottaxamount;//税额 = 含税金额-无税金额

//            data.put("taxamount",taxamount);
////            data.put("nottaxamount",nottaxamount);
////            //更新税额和无税金额
////            handlerService.update("gq_invoicepursummary",data);


                System.out.println("nottaxamount==" + nottaxamount);
                //System.out.println("taxamount==" + taxamount);
                int poslid = MapUtils.getIntValue(data, "poslid");
                int prtid = MapUtils.getIntValue(data, "prtid");
                String sql = "update gq_invoicepursummary set taxamount =" + taxamount + ",nottaxamount=" + nottaxamount + ",pricetaxtotal = " + pricetaxtotal + ",faplymoney="+pricetaxtotal+"  where poslid = " + poslid + " and prtid = " + prtid;
                //String sql = "update gq_invoicepursummary set "  + "nottaxamount=" + nottaxamount + ",pricetaxtotal = "+pricetaxtotal+"  where poslid = " + poslid + " and prtid = " + prtid;
                List<Map<String, Object>> bySql = handlerService.findBySql("select pidate from gq_purchaseinvoice where id='" + prtid + "'");
                if (CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String pidate = MapUtils.getString(map, "pidate");
                    handlerService.updateBySql("update gq_invoicepursummary set pidate='"+pidate+"' where prtid='"+prtid+"'");
                }
                //前台控制，不在进行计算
                //handlerService.updateBySql(sql);

                //子表保存后，更新主表发票金额 5、保存时，需校验主表发票金额=子表价税合计汇总数；需校验每行 税额+无税金额=含税金额；
                String sql2 = "select sum(pricetaxtotal) as pricetaxtotals,sum(receivedqty) as receivedqtys,sum(invoicequantity) as invoicequantitys," +
                        "sum(taxamount) as taxamounts,sum(nottaxamount) as nottaxamounts  from gq_invoicepursummary where prtid = " + prtid;
                List<Map<String, Object>> list = handlerService.findBySql(sql2);
                System.out.println("pricetaxtotals===" + list.get(0).get("pricetaxtotals"));
                //String pricetaxtotal = (String)list.get(0).get("pricetaxtotals");
                BigDecimal pricetaxtotals = new BigDecimal(MapUtils.getString(list.get(0), "pricetaxtotals"));
                BigDecimal receivedqtys = new BigDecimal(MapUtils.getString(list.get(0), "receivedqtys"));
                BigDecimal invoicequantitys = new BigDecimal(MapUtils.getString(list.get(0), "invoicequantitys"));
                BigDecimal taxamounts = new BigDecimal(MapUtils.getString(list.get(0), "taxamounts"));
                BigDecimal nottaxamounts = new BigDecimal(MapUtils.getString(list.get(0), "nottaxamounts"));
                //BigDecimal pricetaxtotals = new BigDecimal(MapUtils.getString(list.get(0), "pricetaxtotals"));
                Map<String, Object> map = new HashMap<>();
                map.put("id", prtid);
                map.put("invoicemoney", pricetaxtotals);
                map.put("invoicequantitys", invoicequantitys);
                map.put("receivedqtys", receivedqtys);
                map.put("taxamounts", taxamounts);
                map.put("nottaxamounts", nottaxamounts);
                map.put("pricetaxtotals", pricetaxtotals);
                handlerService.update("gq_purchaseinvoice", map);
            }else if (isUpdate==true){
                BigDecimal unitcost = new BigDecimal(MapUtils.getString(data, "unitcost"));//含税单价
                //double invoicequantity = MapUtils.getDoubleValue(data, "invoicequantity");//发票数量
                BigDecimal invoicequantity = new BigDecimal(MapUtils.getString(data, "invoicequantity"));//发票数量
                //float taxamount = MapUtils.getFloat(data, "taxamount");//税额
                //float taxrate = MapUtils.getFloat(data, "taxrate");
                BigDecimal taxrate = new BigDecimal(MapUtils.getString(data, "taxrate"));//税额
                //含税金额
                BigDecimal pricetaxtotal = invoicequantity.multiply(unitcost);// unitcost * invoicequantity;//含税单价*发票数量
                //无税金额
                double suil = 1 + taxrate.doubleValue();
                BigDecimal nottaxamount = pricetaxtotal.divide(new BigDecimal(suil), 2, BigDecimal.ROUND_HALF_UP);//无税金额=含税金额/（1+税率）
                //float nottaxamount = pricetaxtotal-taxamount;//无税金额 = 含税金额-税额
                //税额
                BigDecimal taxamount = pricetaxtotal.subtract(nottaxamount);//pricetaxtotal - nottaxamount;//税额 = 含税金额-无税金额

//            data.put("taxamount",taxamount);
////            data.put("nottaxamount",nottaxamount);
////            //更新税额和无税金额
////            handlerService.update("gq_invoicepursummary",data);


                System.out.println("nottaxamount==" + nottaxamount);
                //System.out.println("taxamount==" + taxamount);
                int poslid = MapUtils.getIntValue(data, "poslid");
                int prtid = MapUtils.getIntValue(data, "prtid");
                String sql = "update gq_invoicepursummary set taxamount =" + taxamount + ",nottaxamount=" + nottaxamount + ",pricetaxtotal = " + pricetaxtotal + ",faplymoney="+pricetaxtotal+"  where poslid = " + poslid + " and prtid = " + prtid;
                //String sql = "update gq_invoicepursummary set "  + "nottaxamount=" + nottaxamount + ",pricetaxtotal = "+pricetaxtotal+"  where poslid = " + poslid + " and prtid = " + prtid;
                List<Map<String, Object>> bySql = handlerService.findBySql("select pidate from gq_purchaseinvoice where id='" + prtid + "'");
                if (CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> map = bySql.get(0);
                    String pidate = MapUtils.getString(map, "pidate");
                    handlerService.updateBySql("update gq_invoicepursummary set pidate='"+pidate+"' where prtid='"+prtid+"'");
                }

                handlerService.updateBySql(sql);

                //子表保存后，更新主表发票金额 5、保存时，需校验主表发票金额=子表价税合计汇总数；需校验每行 税额+无税金额=含税金额；
                String sql2 = "select sum(pricetaxtotal) as pricetaxtotals,sum(receivedqty) as receivedqtys,sum(invoicequantity) as invoicequantitys," +
                        "sum(taxamount) as taxamounts,sum(nottaxamount) as nottaxamounts  from gq_invoicepursummary where prtid = " + prtid;
                List<Map<String, Object>> list = handlerService.findBySql(sql2);
                System.out.println("pricetaxtotals===" + list.get(0).get("pricetaxtotals"));
                //String pricetaxtotal = (String)list.get(0).get("pricetaxtotals");
                BigDecimal pricetaxtotals = new BigDecimal(MapUtils.getString(list.get(0), "pricetaxtotals"));
                BigDecimal receivedqtys = new BigDecimal(MapUtils.getString(list.get(0), "receivedqtys"));
                BigDecimal invoicequantitys = new BigDecimal(MapUtils.getString(list.get(0), "invoicequantitys"));
                BigDecimal taxamounts = new BigDecimal(MapUtils.getString(list.get(0), "taxamounts"));
                BigDecimal nottaxamounts = new BigDecimal(MapUtils.getString(list.get(0), "nottaxamounts"));
                //BigDecimal pricetaxtotals = new BigDecimal(MapUtils.getString(list.get(0), "pricetaxtotals"));
                Map<String, Object> map = new HashMap<>();
                map.put("id", prtid);
                map.put("invoicemoney", pricetaxtotals);
                map.put("invoicequantitys", invoicequantitys);
                map.put("receivedqtys", receivedqtys);
                map.put("taxamounts", taxamounts);
                map.put("nottaxamounts", nottaxamounts);
                map.put("pricetaxtotals", pricetaxtotals);
                handlerService.update("gq_purchaseinvoice", map);
            }



        } else if ("purchaseinvoiceApp".equals(app)) {
            if ("gq_purchaseinvoice".equals(objName)) {
                String pinum = MapUtils.getString(data, "pinum");
                String id = MapUtils.getString(data, "id");
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_purchaseinvoice");
                if (CollectionUtils.isNotEmpty(bySql)) {
                    for (int i = 0; i < bySql.size(); i++) {
                        Map<String, Object> map = bySql.get(i);
                        String id1 = MapUtils.getString(map, "id");
                        String pinum1 = MapUtils.getString(map, "pinum");
                        if (pinum1.equals(pinum)) {
                            if (id.equals(id1)) {


                            } else {
                                throw new ServiceException("发票号已存在，请从新输入！");
                            }
                        }
                    }
                }
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select suppliername from gq_purchaseinvoice where id='" + id + "'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    Map<String, Object> map = bySql1.get(0);
                    String suppliername = MapUtils.getString(map, "suppliername");
                    handlerService.updateBySql("update gq_invoicepursummary set suppliername='"+suppliername+"' where prtid ='"+id+"'");
                }
            }
        }
    }
    /*
     * 5、保存时，需校验主表发票金额=子表价税合计汇总数；需校验每行 税额+无税金额=含税金额；
     * */
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("gq_purchaseinvoice".equals(objName)){
            String id = MapUtils.getString(data,"id");
            String where = "";
            if(ToolUtil.isNotEmpty(id)){
                where = " and id !="+id;
            }
            List<Map<String, Object>> list2 = handlerService.findBySql("select id from gq_purchaseinvoice where pinum = '"+data.get("pinum")+"'"+where);
            if(CollectionUtils.isNotEmpty(list2)){
                throw new ServiceException("发票编号已存在，请修改发票编号") ;
            }
        }
//        System.out.println("isUpdate==="+isUpdate);
////        if ("gq_purchaseinvoice".equals(objName)&&isUpdate) {
////            int id = MapUtils.getIntValue(data, "id");
////            System.out.println("保存前id==" + id);
////            if (ToolUtil.isNotEmpty(id)) {
////                BigDecimal invoicemoney = new BigDecimal(MapUtils.getFloatValue(data, "invoicemoney"));//;//发票金额
////                System.out.println("invoicemoney======"+invoicemoney);
////
////                String sql2 = "select * from gq_invoicepursummary where prtid = "+id;
////                List<Map<String, Object>> list2 = handlerService.findBySql(sql2);
////                if(CollectionUtils.isNotEmpty(list2)){
////                    String sql = "select sum(pricetaxtotal) as pricetaxtotals from gq_invoicepursummary where prtid = "+id;
////                    List<Map<String, Object>> list = handlerService.findBySql(sql);
////                    System.out.println("pricetaxtotals==="+list.get(0).get("pricetaxtotals"));
////                    //String pricetaxtotal = (String)list.get(0).get("pricetaxtotals");
////                    BigDecimal pricetaxtotals = new BigDecimal(MapUtils.getString(list.get(0), "pricetaxtotals"));
////                    //float pricetaxtotals = (float)list.get(0).get("pricetaxtotals");
////                    if(invoicemoney.toBigInteger()!=pricetaxtotals.toBigInteger()){
////                        throw new ServiceException("主表发票金额需要等于子表价税合计汇总数") ;
////                    }
////                }
////
////
////
////            }
////        }

    }

    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {


    }

    //提交审批前，判断
    /*
     * 需判断订单累计开票数量不能大于订单累计接收数量，累计开票金额不能大于订单累计接收金额；
     * 需判断累计发票金额不能大于合同金额
     * 需判断累计发票金额不能大于节点金额
     * */
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if ("submit".equals(flowNode.getId()) && op.isFlag()) {
            String sql = "select concode,invoicemoney,receivingamounttax,nodecode from gq_purchaseinvoice where id =" + id;
            List<Map<String, Object>> list = handlerService.findBySql(sql);
            if (CollectionUtils.isNotEmpty(list)) {
                //查询合同台账的合同金额和发票累计金额
                String concode = MapUtils.getString(list.get(0), "concode");
                if (ToolUtil.isNotEmpty(concode)) {
                    //需判断累计发票金额不能大于节点金额  新需求改为跟节点不进行关联
//                    List<Map<String, Object>> list3 = handlerService.findBySql("select invoicemoney  from gq_connodenew where nodecode = '" + list.get(0).get("nodecode") + "' and concode = '" + list.get(0).get("concode") + "'");
//                    if (CollectionUtils.isNotEmpty(list3)) {
//                        BigDecimal invoicemoney2 = new BigDecimal(MapUtils.getString(list.get(0), "invoicemoney"));//本次发票金额
//                        BigDecimal invoicemoney1 = new BigDecimal("0");
//                        if(ToolUtil.isNotEmpty(MapUtils.getString(list3.get(0), "invoicemoney"))){
//                            invoicemoney1=  new BigDecimal(MapUtils.getString(list3.get(0), "invoicemoney"));//累计发票金额
//
//                        }
//                        BigDecimal receivingamounttax = new BigDecimal(MapUtils.getString(list.get(0), "receivingamounttax"));//节点金额
//                        if (invoicemoney1.add(invoicemoney2).compareTo(receivingamounttax) == 1) {
//                            //throw new ServiceException("累计发票金额不能大于合同金额") ;
//                            return "累计发票金额不能大于节点金额";
//                        }
//                    }
                    //需判断累计发票金额不能大于合同金额
                    List<Map<String, Object>> list2 = handlerService.findBySql("select invoicemoney,totalprice  from gq_conaccount where concode = '" + list.get(0).get("concode") + "'");
                    if (CollectionUtils.isNotEmpty(list2)) {
                        BigDecimal invoicemoney1 = new BigDecimal(MapUtils.getString(list2.get(0), "invoicemoney"));
                        BigDecimal invoicemoney2 = new BigDecimal(MapUtils.getString(list.get(0), "invoicemoney"));
                        BigDecimal totalprice = new BigDecimal(MapUtils.getString(list2.get(0), "totalprice"));
                        if (invoicemoney1.add(invoicemoney2).compareTo(totalprice) == 1) {
                            //throw new ServiceException("累计发票金额不能大于合同金额") ;
                            return "累计发票金额不能大于合同金额";
                        }
                    }
                }
            }
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_invoicepursummary where prtid='" + id + "'");
                if (CollectionUtils.isEmpty(bySql)){
                    return "发票信息为空，请选择发票物料！";
                }
        /*
        *   `receivedtotalcost` float(50,2) DEFAULT NULL COMMENT '已接收总额（含税）',
              `receivedqty` float(50,2) DEFAULT NULL COMMENT '已接收数量',
        * */
            String sql2 = "select poslid,itemnum,invoicequantity,pricetaxtotal,ponum from gq_invoicepursummary where prtid = " + id;
            List<Map<String, Object>> list2 = handlerService.findBySql(sql2);


            if (CollectionUtils.isNotEmpty(list2)) {
                for (Map<String, Object> stringObjectMap : list2) {
                    String sql4 = " select sum(invoicequantity) as invoicequantitys ,sum(pricetaxtotal) as pricetaxtotals from gq_invoicepursummary where itemnum ='"+stringObjectMap.get("itemnum")+"' and ponum = '"+stringObjectMap.get("ponum")+"' and prtid in (select id from gq_purchaseinvoice where status ='审批中' or status ='已批准')";
                    List<Map<String, Object>> list4 = handlerService.findBySql(sql4);

                    float invoicequantity = 0;//本次发票数量
                    float pricetaxtotal = 0;//含税金额
                    float invoicequantity1 = 0;//累计开票数量
                    float pricetaxtotal1 = 0;//累计开票金额
                    float receivedtotalcost = 0;//已接收总额
                    float receivedqty = 0;//已接收数量
                    int poslid = MapUtils.getIntValue(stringObjectMap, "poslid");

                    if (ToolUtil.isNotEmpty(stringObjectMap.get("invoicequantity"))) {
                        invoicequantity = MapUtils.getFloat(stringObjectMap, "invoicequantity");
                    }
                    if (ToolUtil.isNotEmpty(stringObjectMap.get("pricetaxtotal"))) {
                        pricetaxtotal = MapUtils.getFloat(stringObjectMap, "pricetaxtotal");
                    }
                    List<Map<String, Object>> list3 = handlerService.findBySql("select itemdesc, receivedtotalcost, receivedqty,invoicequantitys,pricetaxtotals from gq_posline where id = " + poslid);
//                    if (ToolUtil.isNotEmpty(list3.get(0).get("invoicequantitys"))) {
//                        invoicequantity1 = MapUtils.getFloat(list3.get(0), "invoicequantitys");
//                    }
//                    if (ToolUtil.isNotEmpty(list3.get(0).get("pricetaxtotals"))) {
//                        pricetaxtotal1 = MapUtils.getFloat(list3.get(0), "pricetaxtotals");
//                    }
                    //物料名称
                    String itemdesc =  MapUtils.getString(list3.get(0),"itemdesc");
                    if (ToolUtil.isNotEmpty(list4.get(0))&&ToolUtil.isNotEmpty(list4.get(0).get("invoicequantitys"))) {
                        invoicequantity1 = MapUtils.getFloat(list4.get(0), "invoicequantitys");
                    }
                    if (ToolUtil.isNotEmpty(list4.get(0))&&ToolUtil.isNotEmpty(list4.get(0).get("pricetaxtotals"))) {
                        pricetaxtotal1 = MapUtils.getFloat(list4.get(0), "pricetaxtotals");
                    }

                    if (ToolUtil.isNotEmpty(list3.get(0).get("receivedtotalcost"))) {
                        receivedtotalcost = MapUtils.getFloat(list3.get(0), "receivedtotalcost");
                    }
                    if (ToolUtil.isNotEmpty(list3.get(0).get("receivedqty"))) {
                        receivedqty = MapUtils.getFloat(list3.get(0), "receivedqty");
                    }

                    if (receivedqty < (invoicequantity + invoicequantity1)) {
                        //throw new BusinessService("累计开票数量不能大于订单累计接收数量");
                        //throw new BusinessS
                        float shengyu = receivedqty-invoicequantity1;//剩余数量
                        return "发票信息中物料名称为“"+itemdesc+ "”的累计发票数量不能大于订单接收数量，剩余可开票数："+shengyu;
                    }
                    if (receivedtotalcost < (pricetaxtotal1 + pricetaxtotal)) {
                        //throw new ServiceException("累计开票金额不能大于订单累计接收金额");
                        return  "发票信息中物料名称为“"+itemdesc+  "”的累计发票金额不能大于订单已接收金额，剩余可开票金额："+(receivedtotalcost-pricetaxtotal1);
                    }

                }
            }
        }
        //----------------------审批后流程逻辑------------------
        //同步合同发票累计数量
        //同步采购订单发票
        if (flowNode.getId().equals("appval")) {
            boolean isagree = op.isFlag();
            if (isagree) {
                String sql = "select concode,invoicemoney,nodecode,ponum from gq_purchaseinvoice where id =" + id;
                List<Map<String, Object>> list = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(list)) {
                    //更新合同节点累计发票金额
//                    String nodecode = MapUtils.getString(list.get(0), "nodecode");
////                    if (ToolUtil.isNotEmpty(nodecode)) {
////                        String upd = "update gq_connodenew set invoicemoney=invoicemoney+" + list.get(0).get("invoicemoney") + " where nodecode = '" + nodecode + "'";
////                        handlerService.updateBySql(upd);
////                    }
                    //更新合同累计发票金额
                    String concode = MapUtils.getString(list.get(0), "concode");
                    if (ToolUtil.isNotEmpty(concode)) {
                        String upd = "update gq_conaccount set invoicemoney=invoicemoney+" + list.get(0).get("invoicemoney") + " where concode = '" + concode + "'";
                        handlerService.updateBySql(upd);
                    }
                    String ponum = MapUtils.getString(list.get(0), "ponum");
                    String invoicemoney = MapUtils.getString(list.get(0), "invoicemoney");//发票金额
                    if (invoicemoney==null||invoicemoney==""){
                        invoicemoney="0.00";
                    }
                    try {
                        if(ToolUtil.isNotEmpty(ponum)){
                            BigDecimal invoicemoney1 = new BigDecimal(invoicemoney);
                            List<Map<String, Object>> bySql = handlerService.findBySql("select paidamts,unpaidamt from gq_po where ponum ='" + ponum + "'");
                            if (CollectionUtils.isNotEmpty(bySql)){
                                String paidamts = MapUtils.getString(bySql.get(0), "paidamts");//累计已开票金额
                                String unpaidamt = MapUtils.getString(bySql.get(0), "unpaidamt");//待开票金额
                                BigDecimal paidamts1 = new BigDecimal(paidamts);
                                BigDecimal unpaidamt1 = new BigDecimal(unpaidamt);
                                paidamts1=paidamts1.add(invoicemoney1);
                                unpaidamt1=unpaidamt1.subtract(invoicemoney1);
                                handlerService.updateBySql("update gq_po set paidamts='"+paidamts1+"',unpaidamt='"+unpaidamt1+"' where ponum='"+ponum+"'");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                String sql2 = "select poslid,invoicequantity,pricetaxtotal from gq_invoicepursummary where prtid = " + id;
                List<Map<String, Object>> list2 = handlerService.findBySql(sql2);
                if (CollectionUtils.isNotEmpty(list2)) {
                    for (Map<String, Object> stringObjectMap : list2) {
                        float invoicequantity = 0;//发票数量
                        float pricetaxtotal = 0;//含税金额
                        BigDecimal paidamts2 = new BigDecimal("0");
                        BigDecimal unpaidamts2 = new BigDecimal("0");
                        int poslid = MapUtils.getIntValue(stringObjectMap, "poslid");
                        if (ToolUtil.isNotEmpty(stringObjectMap.get("invoicequantity"))) {
                            invoicequantity = MapUtils.getFloat(stringObjectMap, "invoicequantity");
                        }
                        if (ToolUtil.isNotEmpty(stringObjectMap.get("pricetaxtotal"))) {
                            pricetaxtotal = MapUtils.getFloat(stringObjectMap, "pricetaxtotal");//开票金额
                            String pricetaxtotal1 = MapUtils.getString(stringObjectMap, "pricetaxtotal");
                            List<Map<String, Object>> bySql = handlerService.findBySql("select paidamts,unpaidamts from gq_posline where id=" + poslid);
                            if (CollectionUtils.isNotEmpty(bySql)){
                                String paidamts = MapUtils.getString(bySql.get(0), "paidamts");//累计付款金额
                                String unpaidamts = MapUtils.getString(bySql.get(0), "unpaidamts");//待付金额
                                if (StringUtils.isNotEmpty(paidamts)){
                                    BigDecimal paidamts1 = new BigDecimal(paidamts);
                                    BigDecimal bigDecimal = new BigDecimal(pricetaxtotal1);
                                    paidamts1 =paidamts1.add(bigDecimal);
                                    paidamts2=paidamts1;
                                    if (StringUtils.isNotEmpty(unpaidamts)){
                                        BigDecimal unpaidamts1 = new BigDecimal(unpaidamts);
                                        unpaidamts1=unpaidamts1.subtract(bigDecimal);
                                        unpaidamts2=unpaidamts1;
                                    }
                                }else {
                                    BigDecimal paidamts1 = new BigDecimal("0");
                                    BigDecimal bigDecimal = new BigDecimal(pricetaxtotal1);
                                    paidamts1 =paidamts1.add(bigDecimal);
                                    paidamts2=paidamts1;
                                    BigDecimal unpaidamts1 = new BigDecimal(unpaidamts);
                                    unpaidamts1=unpaidamts1.subtract(bigDecimal);
                                    unpaidamts2=unpaidamts1;
                                }
                            }
                        }
                        String upd = "update gq_posline set invoicequantitys=invoicequantitys+" + invoicequantity + ",pricetaxtotals = pricetaxtotals+" + pricetaxtotal + ",paidamts="+paidamts2+",unpaidamts="+unpaidamts2+" where id = " + poslid;
                        handlerService.updateBySql(upd);
                    }
                }
            }
        }

        return super.beforeExcuteFlow(app, id, flowNode, op);
    }
}
